package com.wolfalpha.utron.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.view.SchoolAlertDialog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luodon on 15/8/17.
 */
public class RegisterIndentity extends Fragment {

    //对父Activity的接口
    private RegisterIndentityListener mlistener;
    private EditText et_nick;       //昵称
    private EditText et_school;     //学校

    private Button btnNext;         //下一步

    private List<String> schools = new ArrayList<String>();

    public interface RegisterIndentityListener {
        public void onFinished(String nickname, String scode);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_register_identity, container, false);

        et_nick = (EditText) view.findViewById(R.id.et_nickname);
        et_school = (EditText) view.findViewById(R.id.et_school);
        btnNext = (Button) view.findViewById(R.id.btn_register_next);

        et_school.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final SchoolAlertDialog ad = new SchoolAlertDialog(getActivity());
                ad.setTitle("选择项目发起地");

                ad.setPositiveButton("确认", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (ad.getSelected() == null) {
                            UIHelper.ToastMessage(getActivity(), "您未选择任何选项");
                        } else {
                            System.out.println(ad.getSelected());
                            et_school.setText(ad.getSelected());
                        }
                        ad.dismiss();
                    }
                });

                ad.setNegativeButton("取消", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ad.dismiss();
                    }
                });
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UIHelper.ToastMessage(getActivity(), "您选择了" + et_school.getText().toString());
                String nickname = et_nick.getText().toString().trim();
                if (!TextUtils.isEmpty(nickname)) {
                    mlistener.onFinished(nickname, et_school.getText().toString());
                } else {
                    UIHelper.ToastMessage(getActivity(), "请输入昵称");
                }
            }
        });


        return view;
    }

    public void setIndentityListener(RegisterIndentityListener listener) {
        this.mlistener = listener;
    }

}
